package com.ruoyi.framework.mdc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * RestTemplateConfig:
 *
 * @Author： Lonni
 * @Date： 2025/9/9  15:29
 */
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(TraceInterceptor traceInterceptor) {
        RestTemplate restTemplate = new RestTemplate();

        // 获取现有的拦截器
        List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
        if (interceptors.isEmpty()) {
            interceptors = new ArrayList<>();
        }
        // 添加跟踪拦截器
        interceptors.add(traceInterceptor);
        restTemplate.setInterceptors(interceptors);
        return restTemplate;
    }

}
